<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>33 watch 监控</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <script type="text/javascript" src="../assets/js/vue.js"></script>
</head>

<body>
    <h1> Vue 选项 watch</h1>
    <a href="../index.html">返回</a>
    <hr />
    <div id="app">
        {{message}}
        <hr />
        现在温度是：{{newtem}}<br />
        穿衣建议：{{notice}}
        <br />
        <button @click="uptemp">升高温度</button>
        <button @click="downtemp">降低温度</button>
    </div>
    <script>
        var noticelist = ["短袖", "长褂", "大衣", "零下了，别得瑟了"]
        var app = new Vue({
            el: "#app",
            data: {
                message: "Vue watch",
                temperature: 10,
                notice: "无"
            },
            computed: {
                newtem: function () {
                    return "现在是 " + this.temperature + "度"
                }
            },
            methods: {
                uptemp: function () {
                    this.temperature += 2;
                },
                downtemp: function () {
                    this.temperature -= 2;
                }
            },
            watch: {
                //1. 第一种 写法
                // temperature: function (newval, oldval) {
                //     console.log(newval, oldval);
                //     if (newval > 26) {
                //         //this.notice = "短袖";
                //         this.notice = noticelist[0];
                //     } else if (newval >= 10) {
                //         this.notice = "长褂";
                //     } else if (newval > 0) {
                //         this.notice = "大衣";
                //     } else {
                //         //this.notice = "零下了，别得瑟了";
                //         this.notice = noticelist[3];
                //     }
                // }
            }
        });

        //外部使用 2 第二种写法
        app.$watch("temperature", function (newval, oldval) {
            console.log(newval, oldval);
            if (newval > 26) {
                //this.notice = "短袖";
                this.notice = noticelist[0];
            } else if (newval >= 10) {
                this.notice = "长褂";
            } else if (newval > 0) {
                this.notice = "大衣";
            } else {
                //this.notice = "零下了，别得瑟了";
                this.notice = noticelist[3];
            }
        })

    </script>
</body>

</html>